﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Training.Library.Interfaces;
using Training.Library.DomainObject.Administration.Catalog;
using System.Data.SqlClient;
using System.Data;
using Training.Library.Enum;
using SIPO.Library.DomainObject.Administration.Catalog;

namespace Training.Library.DataAccess.Administration.Catalog
{
    public partial class BagianRepo : ISearchable<Bagian>
    {
        public List<Bagian> GetList(int pageSize, int currentPage, string sortName, string sortOrder,string parameters)
        {
            List<Bagian> tt = new List<Bagian>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat("      BagianID, DepartmentID, NamaBagian, KeteranganBagian, Bagian.CreatedDate, Bagian.CreatedBy, Bagian.UpdatedDate, Bagian.UpdatedBy,Department.DepartmentDesc ");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      master.Bagian INNER JOIN DBJobsDesc.master.Department ON Bagian.DepartmentID = Department.DeptID ");
                    sbQuery.AppendFormat("      {0} ",parameters);
                    sbQuery.AppendFormat(" ) ");
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                Bagian t = new Bagian(true);
                                t.BagianID = dr[Bagian.Fields.BagianID] == DBNull.Value ? 0 : int.Parse(dr[Bagian.Fields.BagianID].ToString());
                                t.DepartmentID = dr[Bagian.Fields.DepartmentID] == DBNull.Value ? (int?)null : int.Parse(dr[Bagian.Fields.DepartmentID].ToString());
                                t.NamaBagian = dr[Bagian.Fields.NamaBagian] == DBNull.Value ? string.Empty : dr[Bagian.Fields.NamaBagian].ToString();
                                t.KeteranganBagian = dr[Bagian.Fields.KeteranganBagian] == DBNull.Value ? string.Empty : dr[Bagian.Fields.KeteranganBagian].ToString();
                                t.CreatedDate = dr[Bagian.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[Bagian.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[Bagian.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[Bagian.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[Bagian.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[Bagian.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[Bagian.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[Bagian.Fields.UpdatedBy].ToString();

                                t.Department.DepartmentDesc = dr[Department.Fields.DepartmentDesc] == DBNull.Value ? string.Empty : dr[Department.Fields.DepartmentDesc].ToString();

                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                       
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM master.Bagian {1}", column,parameters);
                            break;
                       
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }
    }
}
